<?php
include ("simple_html_dom.php");

function bl_decode($str)
{
	$str= str_replace(array("&nbsp;","<br>"),"",$str);
	$html = str_get_html($str);
	$paxname =array();
	$mangxx=array();
	$tempx =$html->find('dl.pax-names ul',0);
	$mangxx['pnr']=TrimStr($html->find('h4.booking-reference span',0)->innertext);
        
	foreach($tempx->find('li') as $element)
        {	
                        $rx=explode(",",$element->innertext);
                        $paxname[]=Vietnamese(substr($rx[0],strpos($rx[0],".")+1));
                        if (count($rx)>1)
                        $paxname[]=Vietnamese($rx[1]);
        }
        
	$giave =TrimStr(str_replace(array(".","USD","VND","AUD","SGD"),"",$html->find('dd.details span.price', 0)->innertext));
			
	$cb = array();
	$dem=0;
	$tempx =$html->find('dl.flight-details',0);
	
	foreach($tempx->find('li.legInfoBS') as $element)
        {	
                $tx                     =   explode(",",TrimStr($element->find('p[id=dept-datetime]',0)->innertext));
                $timedept               =   getTime($tx[0]);
                
                $cb[$dem]['ngaybay']    =   trim($tx[1]);
                $cb[$dem]['sohieu']     =   TrimStr($element->find('p span.flight-no',0)->innertext);

                $cb[$dem]['sbdi']       =   $timedept."&nbsp;".xuly_tensb(TrimStr($element->find('p.origin-destination span',0)->innertext));
                $tx                     =   explode(",",TrimStr($element->find('p[id=arrv-datetime]',0)->innertext));

                $timedept               =   getTime($tx[0]);
                $cb[$dem]['sbden']      =   $timedept."&nbsp;".xuly_tensb(TrimStr($element->find('p.origin-destination span',1)->innertext));
                
                $dem++;
        }
		//31/05/2014-TR2325-14:25&nbsp;SGN-17:20&nbsp;SIN_01/06/2014-TR2716-15:50&nbsp;SIN-21:10&nbsp;PER_27/06/2014-TR2717-21:50&nbsp;PER-03:20&nbsp;SIN_28/06/2014-TR2328-15:30&nbsp;SIN-16:40&nbsp;SGN
	$mangxx['paxname']=$paxname;
	$mangxx['flight']=$cb;
	$mangxx['amount']=$giave;	
	return $mangxx;
}
function xuly_tensb($strs)
{
        $aplist=array(
            'CA MAU'        =>  'CAH',
            'CA MAU AIRPORT'        =>  'CAH',
            'CON DAO'       =>  'VCS',
            'CON DAO AIRPORT'       =>  'VCS',
            'CAN THO'       =>  'VCA',
            'CAN THO AIRPORT'       =>  'VCA',
            'TUY HOA'       =>  'TBB',
            'TUY HOA AIRPORT'       =>  'TBB',
            'PLEIKU'        =>  'PXU',
            'DIEN BIEN'     =>  'DIN',
            'DA LAT'        =>  'DLI',
            'DA LAT AIRPORT'        =>  'DLI',
            'CHU LAI'       =>  'VCL',
            'CHU LAI AIRPORT'       =>  'VCL',
            'QUI NHON'      =>  'UIH',
            'QUI NHON AIRPORT'=>  'UIH',
            'DONG HOI'      =>  'VDH',
            'DONG HOI AIRPORT'      =>  'VDH',
            'THANH HOA'     =>  'THD',
            'THANH HOA AIRPOT'     =>  'THD',
            'BUON MA THUOT' =>  'BMV',
            'DA NANG'       =>  'DAD',
            'DA NANG AIRPORT'       =>  'DAD',
            'HA NOI'        =>  'HAN',
            'HAI PHONG'     =>  'HPH',
            'HUE'           =>  'HUI',
            'NHA TRANG'=>'CXR',
            'NHA TRANG AIRPORT'=>'CXR',
            'PHU QUOC'=>'PQC',
            'PHU QUOC AIRPORT'=>'PQC',
            'HO CHI MINH'=>'SGN',
            'VINH'=>'VII',
            'ADELAIDE'=>'ADL',
            'AIRLIE BEACH'=>'WSY',
            'ALBANY'=>'PER',
            'AYERS ROCK (ULURU)'=>'AYQ',
            'BALLINA BYRON'=>'BNK',
            'BAROSSA VALLEY'=>'ADL',
            'BRAMPTON ISLAND'=>'MKY',
            'BRISBANE'=>'BNE',
            'BUSSELTON'=>'PER',
            'CAIRNS'=>'CNS',
            'CRADLE MOUNTAIN'=>'HBA',
            'DARWIN'=>'DRW',
            'DAYDREAM ISLAND'=>'DDI',
            'DAYLESFORD'=>'MEL',
            'FALLS CREEK'=>'MEL',
            'GOLD COAST'=>'OOL',
            'HAMILTON ISLAND'=>'HTI',
            'HAYMAN ISLAND'=>'HIS',
            'HOBART'=>'HBA',
            'HUNTER VALLEY'=>'NTL',
            'KAKADU'=>'DRW',
            'KINGSCLIFF'=>'OOL',
            'LAUNCESTON'=>'LST',
            'LONG ISLAND'=>'HAP',
            'LORNE'=>'AVV',
            'MACKAY'=>'MKY',
            'MARGARET RIVER'=>'PER',
            'MELBOURNE (ALL AIRPORTS)'=>'VIZ',
            'MELBOURNE (AVALON)'=>'AVV',
            'MELBOURNE (TULLAMARINE)'=>'MEL',
            'MORNINGTON PENINSULA'=>'MEL',
            'NEWCASTLE  PORT STEPHENS'=>'NTL',
            'NOOSA'=>'MCY',
            'PALM COVE'=>'CNS',
            'PERTH'=>'PER',
            'PORT DOUGLAS'=>'CNS',
            'PORT STEPHENS'=>'NTL',
            'ROCKHAMPTON'=>'ROK',
            'SUNSHINE COAST'=>'MCY',
            'SYDNEY'=>'SYD',
            'TORQUAY'=>'AVV',
            'TOWNSVILLE'=>'TSV',
            'WARRNAMBOOL'=>'AVV',
            'WHITSUNDAY COAST - PROSERPINE'=>'PPP',
            'YARRA VALLEY'=>'MEL',
            'YEPOON'=>'ROK',
            'PHNOM PENH'=>'PNH',
            'SIEM REAP'=>'REP',
            'BEIJING'=>'PEK',
            'GUANGZHOU'=>'CAN',
            'HAIKOU'=>'HAK',
            'HANGZHOU'=>'HGH',
            'HGH'=>'HGH',
            'KWL'=>'KWL',
            'NINGBO'=>'NGB',
            'NNG'=>'NNG',
            'SHANTOU / JIEYANG'=>'SWA',
            'FIJI - BEACHCOMBER'=>'NAN',
            'FIJI - DENARAU'=>'NAN',
            'FIJI - INTERCON'=>'',
            'FIJI - OUTRIGGER'=>'NAN',
            'FIJI - SONAISALI ISLAND'=>'NAN',
            'FIJI - THE NAVITI'=>'NAN',
            'FIJI - TREASURE ISLAND'=>'NAN',
            'FIJI - WARWICK'=>'NAN',
            'NADI'=>'NAN',
            'HONG KONG'=>'HKG',
            'BALI BENOA'=>'DPS',
            'BALI JIMBARAN BAY'=>'DPS',
            'BALI KUTA LEGIAN'=>'DPS',
            'BALI NUSA DUA'=>'DPS',
            'BALI SANUR'=>'DPS',
            'BALI SEMINYAK'=>'DPS',
            'BALI UBUD'=>'DPS',
            'BALI ULUWATU'=>'DPS',
            'DENPASAR (BALI)'=>'DPS',
            'JAKARTA'=>'CGK',
            'LOMBOK'=>'LOP',
            'MANDALIKA'=>'LOP',
            'MEDAN'=>'KNO',
            'SENGGIGI'=>'LOP',
            'SURABAYA'=>'SUB',
            'FUKUOKA'=>'FUK',
            'KAGOSHIMA'=>'KOJ',
            'KANSAI'=>'KIX',
            'KYOTO'=>'KYO',
            'MATSUYAMA'=>'MYJ',
            'NAGOYA (CHUBU)'=>'NGO',
            'NARITA'=>'NRT',
            'OITA'=>'OIT',
            'OKINAWA (NAHA)'=>'OKA',
            'OSAKA'=>'KIX',
            'SAPPORO (NEW CHITOSE AIRPORT)'=>'CTS',
            'TAKAMATSU'=>'TAK',
            'TOKYO'=>'NRT',
            'MA CAO'=>'MFM',
            'KUALA LUMPUR'=>'KUL',
            'KUCHING'=>'KCH',
            'LANGKAWI'=>'KUL',
            'PENANG'=>'PEN',
            'YANGON'=>'RGN',
            'AUCKLAND'=>'AKL',
            'AUCKLAND'=>'ROT',
            'CHRISTCHURCH'=>'CHC',
            'DUNEDIN'=>'DUD',
            'QUEENSTOWN'=>'ZQN',
            'WANAKA'=>'CHC',
            'WELLINGTON'=>'WLG',
            'MANILA'=>'MNL',
            'SENTOSA'=>'SIN',
            'SINGAPORE'=>'SIN',
            'SEOUL'=>'ICN',
            'TAIPEI'=>'TPE',
            'BANGKOK'=>'BKK',
            'PHUKET'=>'HKT',
            'HONOLULU'=>'HNL'
        );
        $strs       =   Vietnamese($strs);
	$strs       =   str_replace("THANH PHO ","",$strs);
	return $aplist[$strs];
}

function TrimStr($str)
{
    $str        = trim($str);
    $ret_str    = "";
    for($i=0;$i < strlen($str);$i++)
    {

        if(substr($str, $i, 1) != " ")
        {

            $ret_str .= trim(substr($str, $i, 1));

        }
        else
        {
            while(substr($str,$i,1) == " ")

            {
                $i++;
            }
            $ret_str.= " ";
            $i--; // ***
        }
    }
    return $ret_str;
}
function Vietnamese($str_cv){
	$marTViet=array("à","á","ạ","ả","ã","â","ầ","ấ","ậ","ẩ","ẫ","ă",
            "ằ","ắ","ặ","ẳ","ẵ","è","é","ẹ","ẻ","ẽ","ê","ề"
            ,"ế","ệ","ể","ễ",
            "ì","í","ị","ỉ","ĩ",
            "ò","ó","ọ","ỏ","õ","ô","ồ","ố","ộ","ổ","ỗ","ơ"
            ,"ờ","ớ","ợ","ở","ỡ",
            "ù","ú","ụ","ủ","ũ","ư","ừ","ứ","ự","ử","ữ",
            "ỳ","ý","ỵ","ỷ","ỹ",
            "đ",
            "À","Á","Ạ","Ả","Ã","Â","Ầ","Ấ","Ậ","Ẩ","Ẫ","Ă"
            ,"Ằ","Ắ","Ặ","Ẳ","Ẵ",
            "È","É","Ẹ","Ẻ","Ẽ","Ê","Ề","Ế","Ệ","Ể","Ễ",
            "Ì","Í","Ị","Ỉ","Ĩ",
            "Ò","Ó","Ọ","Ỏ","Õ","Ô","Ồ","Ố","Ộ","Ổ","Ỗ","Ơ"
            ,"Ờ","Ớ","Ợ","Ở","Ỡ",
            "Ù","Ú","Ụ","Ủ","Ũ","Ư","Ừ","Ứ","Ự","Ử","Ữ",
            "Ỳ","Ý","Ỵ","Ỷ","Ỹ",
            "Đ");

            $marKoDau=array("a","a","a","a","a","a","a","a","a","a","a"
            ,"a","a","a","a","a","a",
            "e","e","e","e","e","e","e","e","e","e","e",
            "i","i","i","i","i",
            "o","o","o","o","o","o","o","o","o","o","o","o"
            ,"o","o","o","o","o",
            "u","u","u","u","u","u","u","u","u","u","u",
            "y","y","y","y","y",
            "d",
            "A","A","A","A","A","A","A","A","A","A","A","A"
            ,"A","A","A","A","A",
            "E","E","E","E","E","E","E","E","E","E","E",
            "I","I","I","I","I",
            "O","O","O","O","O","O","O","O","O","O","O","O"
            ,"O","O","O","O","O",
            "U","U","U","U","U","U","U","U","U","U","U",
            "Y","Y","Y","Y","Y",
            "D");
            return strtoupper(str_replace($marTViet,$marKoDau,$str_cv));
}

function getTime($str){
    $string = trim(substr($str,-5));
    if(strlen($string) < 5){
        $string = '0'.$string;
    }
    return $string;
}